﻿using System;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using MySql.Data.MySqlClient;
using MySql.Data.Types;


namespace EmailMarketingCampaign
{    
    public class DataSource
    {
        MySqlConnection connection;
        MySqlCommand command;
        MySqlDataReader dataReader;

        public DataSource()
        {
            connection = new MySqlConnection(ConfigurationManager.ConnectionStrings["EmailMarketing_DBConnectionString"].ConnectionString);
        }

        public void ExecuteInsert(MySqlCommand scd)
        {
            scd.Connection = connection;
            connection.Open();
            scd.ExecuteNonQuery();
            connection.Close();
        }

        public int ExecuteNonQuery(String query)
        {
            int rowAffected = 0;
            connection.Open();
            command = new MySqlCommand(query, connection);
            rowAffected = command.ExecuteNonQuery();
            connection.Close();
            return rowAffected;
        }
        public MySqlDataReader ExecuteReader(String query)
        {
            connection.Open();
            command = new MySqlCommand(query, connection);
            return command.ExecuteReader();
        }
        public void CloseConnection()
        {
            connection.Close();
        }
        public void CloseReader()
        {
            dataReader.Close();
        }

        public DataTable GetDataSet(string sql, string tableName)
        {
            connection.Open();
            DataSet ds = new DataSet();
            MySqlDataAdapter da = new MySqlDataAdapter(sql, connection);
            da.Fill(ds, tableName);
            connection.Close();
            return ds.Tables[tableName];
        }
    }
}